package com.jianbing.north.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

import com.jianbing.north.domain.NorthUserRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.north.mapper.NorthPart3RecordMapper;
import com.jianbing.north.domain.NorthPart3Record;
import com.jianbing.north.service.INorthPart3RecordService;

/**
 * part3录音文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-22
 */
@Service
public class NorthPart3RecordServiceImpl implements INorthPart3RecordService 
{
    @Autowired
    private NorthPart3RecordMapper northPart3RecordMapper;

    /**
     * 查询part3录音文件
     * 
     * @param id part3录音文件主键
     * @return part3录音文件
     */
    @Override
    public NorthPart3Record selectNorthPart3RecordById(Long id)
    {
        return northPart3RecordMapper.selectNorthPart3RecordById(id);
    }

    /**
     * 查询part3录音文件列表
     * 
     * @param northPart3Record part3录音文件
     * @return part3录音文件
     */
    @Override
    public List<NorthPart3Record> selectNorthPart3RecordList(NorthPart3Record northPart3Record)
    {
        return northPart3RecordMapper.selectNorthPart3RecordList(northPart3Record);
    }

    /**
     * 新增part3录音文件
     * 
     * @param northPart3Record part3录音文件
     * @return 结果
     */
    @Override
    public int insertNorthPart3Record(NorthPart3Record northPart3Record)
    {
        return northPart3RecordMapper.insertNorthPart3Record(northPart3Record);
    }

    /**
     * 修改part3录音文件
     * 
     * @param northPart3Record part3录音文件
     * @return 结果
     */
    @Override
    public int updateNorthPart3Record(NorthPart3Record northPart3Record)
    {
        return northPart3RecordMapper.updateNorthPart3Record(northPart3Record);
    }

    /**
     * 批量删除part3录音文件
     * 
     * @param ids 需要删除的part3录音文件主键
     * @return 结果
     */
    @Override
    public int deleteNorthPart3RecordByIds(Long[] ids)
    {
        return northPart3RecordMapper.deleteNorthPart3RecordByIds(ids);
    }

    /**
     * 删除part3录音文件信息
     * 
     * @param id part3录音文件主键
     * @return 结果
     */
    @Override
    public int deleteNorthPart3RecordById(Long id)
    {
        return northPart3RecordMapper.deleteNorthPart3RecordById(id);
    }

    @Override
    public List<NorthUserRecordDTO> selectNorthRecordSummaryList(Long userId) {
        List<NorthUserRecordDTO> northUserRecordDTOS = northPart3RecordMapper.selectNorthRecordSummaryList(userId);
        for(NorthUserRecordDTO northUserRecordDTO:northUserRecordDTOS){

            if(northUserRecordDTO.getTimes()<5){
                northUserRecordDTO.setEndTime(null);
            }else{
                Date begin = northUserRecordDTO.getBeginTime();
                Date end = northUserRecordDTO.getEndTime();
                Date date = null;
                String timeFormat = null;
                if(begin!=null && end!=null){
                    Duration duration = Duration.between(begin.toInstant(),end.toInstant());
                    Long time = duration.getSeconds();
                    timeFormat = String.format("%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60);
                    try {
                        date = new SimpleDateFormat("HH:mm:ss").parse(timeFormat);
                    }catch (ParseException exception){
                        date = null;
                    }
                }
                northUserRecordDTO.setDuration(date);
            }
        }
        return northUserRecordDTOS;
    }

    @Override
    public List<Long> selectDialogueId() {
        return northPart3RecordMapper.selectDialogueId();
    }

    @Override
    public List<Long> selectUserId() {
        return northPart3RecordMapper.selectUserId();
    }

    @Override
    public List<NorthPart3Record> selectP3(NorthPart3Record northPart3Record) {
        return northPart3RecordMapper.selectP3(northPart3Record);
    }
}
